#include <iostream>
#include <array>

using namespace std;

constexpr static array<int, 10> arr { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };

static bool func(int a) {
    int b = a;
    int s = 0;
    while (b) {
        s += arr[b % 10];
        b /= 10;
    }
    return s == a;
}

int main() {
    int s = 0;
    for (int i {10}; i < 1e8; ++i) {
        if (func(i)) {
            s += i;
            cout << i << ' ';
        }
    }
    cout << endl;
    cout << s << endl; // 40730
}